//
//  ListViewController.m
//  ShangAPP
//
//  Created by 徐番茄丶 on 16/1/22.
//  Copyright © 2016年 wangdao. All rights reserved.
//

#import "ListViewController.h"
#import "MyTools.h"
#import "ListTableViewCell.h"
#import <MJRefresh.h>

@interface ListViewController ()<UITableViewDataSource,
                                   UITableViewDelegate>
{
    UITableView      * _myTab;
    NSArray          * _myarr;
}

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"提现记录";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0x444444)];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"刷新"style:UIBarButtonItemStylePlain target:self action:@selector(shuaxin)];
//    [self.navigationItem.rightBarButtonItem setTintColor:UIColorFromRGB(0x444444)];
    
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
    
    _myarr =[[NSArray alloc]init];
    
    [self viewCreate];
}
- (void)shuaxin
{
    [self getDate];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

-(void)getDate{
    NSString *url = @"http://www.xsy777.com/app/merchant/withdrawlist.html";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = @{@"user_token":[user objectForKey:@"token"]};
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [manager POST:url parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            _myarr = [[NSArray alloc]initWithArray:[[responseObject objectForKey:@"data"] objectForKey:@"list"]];
            
            
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        
        [self viewCreate];
        [_myTab reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}

-(void)viewCreate{
    _myTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _myTab.rowHeight = 50;
    _myTab.backgroundColor = UIColorFromRGB(0xefeff1);
    _myTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTab.showsVerticalScrollIndicator = NO;
//    _myTab.scrollEnabled = NO;
    [_myTab addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(shuaxin)];
    _myTab.delegate = self;
    _myTab.dataSource = self;
    [self.view addSubview:_myTab];
}

#pragma mark tableview代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _myarr.count+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    ListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[ListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    cell.userInteractionEnabled = NO;
    if (indexPath.row == 0) {
        cell.timelab.textColor = UIColorFromRGB(0x333333);
        cell.moneylab.textColor = UIColorFromRGB(0x333333);
        cell.numlab.textColor = UIColorFromRGB(0x333333);
        cell.statelab.textColor = UIColorFromRGB(0x333333);
        cell.typelab.backgroundColor = [UIColor clearColor];
        
        cell.timelab.font = [UIFont systemFontOfSize:15];
        cell.moneylab.font = [UIFont systemFontOfSize:15];
        cell.numlab.font = [UIFont systemFontOfSize:15];
        cell.statelab.font = [UIFont systemFontOfSize:15];
        
        cell.timelab.text = @"日期";
        cell.moneylab.text = @"金额";
        cell.numlab.text = @"账户";
        cell.statelab.text = @"状态";
        
    }else{
        NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:[_myarr objectAtIndex:indexPath.row - 1]];
        
        if (![[dic objectForKey:@"create_time"]isEqual:[NSNull null]]){
            cell.timelab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"create_time"]];
        }else{
            cell.timelab.text =@"";
        }
        if (![[dic objectForKey:@"price"]isEqual:[NSNull null]]){
            cell.moneylab.text = [NSString stringWithFormat:@"￥%@",[dic objectForKey:@"price"]];
        }else{
            cell.typelab.text =@"";
        }
        if (![[dic objectForKey:@"status_title"]isEqual:[NSNull null]]){
            cell.statelab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"status_title"]];
        }else{
            cell.statelab.text =@"";
        }
        if (![[dic objectForKey:@"pay_status_title"]isEqual:[NSNull null]]){
            
            if ([[dic objectForKey:@"pay_status_title"] isEqualToString:@"支付宝"]) {
                if (![[dic objectForKey:@"alipay"]isEqual:[NSNull null]]){
                    cell.numlab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"alipay"]];
                }else{
                    cell.numlab.text =@"";
                }
//                cell.numlab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"alipay"]];
                cell.typelab.text =@"支";
                cell.typelab.backgroundColor = UIColorFromRGB(0x03A9F4);
            }else if ([[dic objectForKey:@"pay_status_title"] isEqualToString:@"微信"]){
                if (![[dic objectForKey:@"accounts"]isEqual:[NSNull null]]){
                    cell.numlab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"accounts"]];
                }else{
                    cell.numlab.text =@"";
                }
//                cell.numlab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"accounts"]];
                cell.typelab.text =@"微";
                cell.typelab.backgroundColor = UIColorFromRGB(0x8BC34A);
            }
            
        }else{
            cell.numlab.text =@"未知";
            cell.typelab.text =@"未知";
            cell.typelab.backgroundColor = UIColorFromRGB(0x8BC34A);
        }
        
    }
    return cell;
}


-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    [self getDate];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
